import database.didactic.ActivityDao;
import database.didactic.ActivityTypeDao;
import database.didactic.InformationSourceDao;
import database.util.ArrayListToXML;
import model.didactic.Activity;
import model.didactic.ActivityType;
import model.didactic.InformationSource;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class SporteliCommonResources extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestedResource = request.getParameter("resource");
        PrintWriter out = response.getWriter();

        if(requestedResource==null || requestedResource.trim().length()<1){
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Kerkesa nuk eshte e sakte");
        }else {
            if (requestedResource.equalsIgnoreCase("activityTypes")){
                ArrayList<ActivityType> arrayList=ActivityTypeDao.getActivityTypes();
                if (arrayList!= null && arrayList.size()>0){
                    String xmlResult = ArrayListToXML.toActivityTypesXML(arrayList);
                    if (xmlResult!=null && xmlResult.trim().length()>0){
                        response.setStatus(HttpServletResponse.SC_OK);
                        out.println(xmlResult);
                    } else {
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ndodhi nje gabim ne server!");
                    }
                } else {
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    response.sendError(HttpServletResponse.SC_NO_CONTENT,"Kerkesa nuk dha asnje rezultat!");
                }

            } else if (requestedResource.equalsIgnoreCase("activities")){
                ArrayList<Activity> arrayList=ActivityDao.getActivities();
                if (arrayList!= null && arrayList.size()>0){
                    String xmlResult = ArrayListToXML.toActivitiesXML(arrayList);
                    if (xmlResult!=null && xmlResult.trim().length()>0){
                        response.setStatus(HttpServletResponse.SC_OK);
                        out.println(xmlResult);
                    } else {
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ndodhi nje gabim ne server!");
                    }
                } else {
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    response.sendError(HttpServletResponse.SC_NO_CONTENT,"Kerkesa nuk dha asnje rezultat!");
                }

            } else if (requestedResource.equalsIgnoreCase("informationSources")){
                ArrayList<InformationSource> arrayList=InformationSourceDao.getSources();
                if (arrayList!= null && arrayList.size()>0){
                    String xmlResult = ArrayListToXML.toInformationSourcesXML(arrayList);
                    if (xmlResult!=null && xmlResult.trim().length()>0){
                        response.setStatus(HttpServletResponse.SC_OK);
                        out.println(xmlResult);
                    } else {
                        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Ndodhi nje gabim ne server!");
                    }
                } else {
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    response.sendError(HttpServletResponse.SC_NO_CONTENT,"Kerkesa nuk dha asnje rezultat!");
                }

            } else if(requestedResource.equalsIgnoreCase("towns")){

            } else {
                response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
                response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED,"Resoursi i kerkuarn nuk eshte implementuar");
            }
        }
    }


    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
